{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4d9adca2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-29T13:21:40.887942Z",
     "start_time": "2023-11-29T13:21:39.487166Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import torch"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13a6e435",
   "metadata": {},
   "source": [
    "## np.moveaxis"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ad1dbba",
   "metadata": {},
   "source": [
    "- `numpy.moveaxis(a, source, destination)`\n",
    "    - Move axes of an array to new positions.\n",
    "    - Other axes remain in their original order."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "86711746",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-17T12:57:01.089686Z",
     "start_time": "2023-10-17T12:57:01.080869Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.zeros((2, 3, 4))\n",
    "# len(source) == len(destination) 时一一对应\n",
    "# 0 -> 2: [, , 2]\n",
    "# 1 -> 0: [3, , ]\n",
    "# 2 -> 1: [, 4, ]\n",
    "# [2, 3, 4] == [3, 4, 2]\n",
    "np.moveaxis(x, (0, 1, 2), (2, 0, 1)).shape == (3, 4, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9144d3e9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-17T12:58:03.573598Z",
     "start_time": "2023-10-17T12:58:03.564601Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# axis 1 移动到 axis 2\n",
    "# 1 -> 2: [, , 3]\n",
    "# 其他相对顺序保持不变，2, 4 => (2, 4)\n",
    "# [2, 4, 3]\n",
    "np.moveaxis(x, 1, 2).shape == (2, 4, 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "415b4fb5",
   "metadata": {},
   "source": [
    "## 增加一个轴（维度）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "471adfca",
   "metadata": {},
   "source": [
    "### np.newaxis"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "377f389e",
   "metadata": {},
   "source": [
    "- 在 `np.newaxis` 出现的位置 shape 为 1；\n",
    "    - `np.newaxis` 相等于 `None`\n",
    "        - 二者是等价的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9c7cc8ab",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-06T07:11:53.919566Z",
     "start_time": "2023-10-06T07:11:53.903162Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4,)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.random.randn(4)\n",
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "061e9e21",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-06T07:11:55.654681Z",
     "start_time": "2023-10-06T07:11:55.645314Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 4)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[np.newaxis, :].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "099633f0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-06T07:12:01.435398Z",
     "start_time": "2023-10-06T07:12:01.425936Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 4)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[None, :].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d05a1ae8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-04T05:35:06.583839Z",
     "start_time": "2023-10-04T05:35:06.579084Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 1, 4)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[np.newaxis, np.newaxis, :].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9c06cd2d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-04T05:36:01.913731Z",
     "start_time": "2023-10-04T05:36:01.909202Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 4, 1)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[np.newaxis, :, np.newaxis].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bdb79f6d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-04T05:36:15.263199Z",
     "start_time": "2023-10-04T05:36:15.105977Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "too many indices for array: array is 1-dimensional, but 2 were indexed",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m/var/folders/1j/fv7y5kz53592pt9xx4tqfrl80000gn/T/ipykernel_20849/2641761860.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mx\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnewaxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnewaxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m: too many indices for array: array is 1-dimensional, but 2 were indexed"
     ]
    }
   ],
   "source": [
    "x[np.newaxis, :, np.newaxis, :].shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f00e033",
   "metadata": {},
   "source": [
    "### torch.unsqueeze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9b539a65",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-06T07:13:13.301997Z",
     "start_time": "2023-10-06T07:13:13.264948Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.8904, 0.7389, 0.3453, 0.2910, 0.5067])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.rand(5)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3210aac3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-06T07:13:33.056277Z",
     "start_time": "2023-10-06T07:13:33.045885Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 5])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.unsqueeze(x, 0).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "02313e37",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-06T07:13:42.032526Z",
     "start_time": "2023-10-06T07:13:42.023422Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 5])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x[None, :].shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9f78763",
   "metadata": {},
   "source": [
    "## examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bf84625a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-29T13:21:44.687027Z",
     "start_time": "2023-11-29T13:21:44.682277Z"
    }
   },
   "outputs": [],
   "source": [
    "A = torch.randn(3, 4, 4, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2f277655",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-29T13:21:48.806147Z",
     "start_time": "2023-11-29T13:21:48.796002Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 4, 4])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[:, :, :, 0].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "db3df01d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-29T13:21:57.933287Z",
     "start_time": "2023-11-29T13:21:57.926686Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 1, 4, 4])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[:, :, :, 0][:, None, :, :].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "462d36ce",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-29T13:21:59.553655Z",
     "start_time": "2023-11-29T13:21:59.545042Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 6, 4, 4])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[:, :, :, 0][:, None, :, :].repeat(1, 6, 1, 1).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d8dab4f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
